package edu.uwm.cs552;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class ScaleComboBox extends JComboBox {

	/**
	 * Keep Eclipse happy
	 */
	private static final long serialVersionUID = 1L;
	
	private static Integer[] scales = new Integer[] {
		10, 15, 20, 30, 40, 50, 60, 75, 100, 120, 150, 200
	};
	private static Integer defaultScale = 20;

	private final HexBoardPanel panel;
	
	public ScaleComboBox(HexBoardPanel p) {
		super(scales);
		panel = p;
		this.setMaximumSize(this.getPreferredSize());
		this.setEditable(true);
		this.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				scaleChangeRequested();
			}
		});
		this.setSelectedItem(defaultScale);
	}
	
	public void scaleChangeRequested() {
		double newScale = defaultScale;
		Object x = super.getSelectedItem();
		if (x instanceof String) {
			try {
				newScale = Float.parseFloat((String)x);
			} catch (NumberFormatException e) {
				super.setSelectedItem(newScale = defaultScale);
			}
		} else if (x instanceof Number) {
			newScale = ((Number)x).doubleValue();
		}
		if (newScale < HexBoardPanel.MIN_SCALE) {
			JOptionPane.showMessageDialog(this, "Scale must be at least " + HexBoardPanel.MIN_SCALE, "Scale error", JOptionPane.ERROR_MESSAGE);
			super.setSelectedItem(newScale = defaultScale);
		}
		panel.setScale(newScale);
	}
	

}
